package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.AAttack;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.IAAttackService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.web.controller.tool.CalcUtils;
import com.ruoyi.web.controller.tool.Rate;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 攻击评估Controller
 *
 * @author ruoyi
 * @date 2020-07-08
 */
@Controller
@RequestMapping("/system/attack")
public class AAttackController extends BaseController {
    private String prefix = "system/attack";
    private String addPrefix = "system/attack_add";

    @Autowired
    private IAAttackService aAttackService;

    @Autowired
    private ISysConfigService configService;

    @RequiresPermissions("system:attack:view")
    @GetMapping()
    public String attack() {
        return prefix + "/attack";
    }


    @GetMapping("/attack_add")
    public String attackAdd(ModelMap mmap) {
        SysConfig sysConfig = configService.selectConfigById(new Long(5));

        try {
            String[] split = sysConfig.getConfigValue().split(":");
            mmap.put("config", sysConfig);
            mmap.put("v1", split[0]);
            mmap.put("v2", split[1]);
            mmap.put("v3", split[2]);
        } catch (Exception e) {

        }

        return addPrefix + "/add";
    }

    /**
     * 查询攻击评估列表
     */
    @RequiresPermissions("system:attack:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AAttack aAttack) {
        startPage();
        List<AAttack> list = aAttackService.selectAAttackList(aAttack);
        return getDataTable(list);
    }

    /**
     * 导出攻击评估列表
     */
    @RequiresPermissions("system:attack:export")
    @Log(title = "攻击评估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AAttack aAttack) {
        List<AAttack> list = aAttackService.selectAAttackList(aAttack);
        ExcelUtil<AAttack> util = new ExcelUtil<AAttack>(AAttack.class);
        return util.exportExcel(list, "attack");
    }

    /**
     * 新增攻击评估
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存攻击评估
     */
    @RequiresPermissions("system:attack:add")
    @Log(title = "攻击评估", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AAttack aAttack) {
        int code = aAttackService.insertAAttack(aAttack);
        aAttack.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(code);
    }

    /**
     * 修改攻击评估
     */
    @GetMapping("/edit/{aId}")
    public String edit(@PathVariable("aId") Long aId, ModelMap mmap) {
        AAttack aAttack = aAttackService.selectAAttackById(aId);
        mmap.put("aAttack", aAttack);
        return prefix + "/edit";
    }


    /**
     * 修改保存攻击评估
     */
    @RequiresPermissions("system:attack:edit")
    @Log(title = "攻击评估", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AAttack aAttack) {
        return toAjax(aAttackService.updateAAttack(aAttack));
    }

    /**
     * 删除攻击评估
     */
    @RequiresPermissions("system:attack:remove")
    @Log(title = "攻击评估", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(aAttackService.deleteAAttackByIds(ids));
    }


    @PostMapping("/getCanKaoData")
    @ResponseBody
    public TableDataInfo getCanKaoData(int count) {
        ArrayList<Map> maps = new ArrayList<>();
        TableDataInfo dataInfo = new TableDataInfo();


        dataInfo.setRows(maps);
        dataInfo.setTotal(0);
        dataInfo.setCode(0);
        //计算gailv
        List<Rate> rates = CalcUtils.calcGls(count);
        dataInfo.setOther(rates);

        Map m1 = new HashMap();
        m1.put("title", "比率");
        Map m2 = new HashMap();
        m2.put("title", "累计比率");

        for (Rate r : rates) {
            m1.put(r.getV1(), r.getV2());
            m2.put(r.getV1(), r.getV3());
        }
        maps.add(m1);
        maps.add(m2);

        return dataInfo;
    }


    /**
     * 评估详情
     */
    @GetMapping("/info/{aId}")
    public String info(@PathVariable("aId") Long aId, ModelMap mmap) {
        AAttack aAttack = aAttackService.selectAAttackById(aId);
        mmap.put("aAttack", aAttack);
        return prefix + "/info";
    }

}
